﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace com.lover.astd.common.manager
{
    public class LogHelper
    {
        private string _dirName = "";
        private string _logFileName = "astd.log";
        public LogHelper(string partner, int serverid, string rolename)
        {
            string top_dir = "accounts";
            string pure_dir = String.Format("{0}_{1}_{2}", partner, serverid, rolename);
            _dirName = String.Format("{0}/{1}", top_dir, pure_dir);
            if (_dirName == null || _dirName.Trim().Length == 0) return;
            DirectoryInfo dirinfo = new DirectoryInfo(_dirName);
            if (!dirinfo.Exists)
            {
                // 判断是否有以前的文件夹存在, 如果有, 则改名
                string dirname_too_old = rolename;
                string dirname_former = String.Format("{0}/{1}", top_dir, rolename);
                if (Directory.Exists(dirname_too_old))
                {
                    if (!Directory.Exists(top_dir)) Directory.CreateDirectory(top_dir);
                    Directory.Move(dirname_too_old, _dirName);
                }
                if (Directory.Exists(dirname_former))
                {
                    Directory.Move(dirname_former, _dirName);
                }
                if (!Directory.Exists(_dirName)) dirinfo.Create();
                if (!Directory.Exists(_dirName + "/logs")) dirinfo.Create();
            }else{
                dirinfo = new DirectoryInfo(_dirName + "/logs");
                if (!dirinfo.Exists) dirinfo.Create();
            }
        }
        public void log(string logtext)
        {
            if (_dirName == null || _dirName.Trim().Length == 0) return;
            string path = "logs/" + _logFileName;
            if (_dirName.Trim().Length > 0)
            {
                DirectoryInfo dirinfo = new DirectoryInfo(_dirName);
                if (!dirinfo.Exists)
                {
                    dirinfo.Create();
                    dirinfo = new DirectoryInfo(_dirName + "/logs");
                    dirinfo.Create();
                }
                else
                {
                    dirinfo = new DirectoryInfo(_dirName + "/logs");
                    if (!dirinfo.Exists) dirinfo.Create();
                }
                path = _dirName + "/" + path;
            }
            FileStream stream = null;
            FileInfo finfo = new FileInfo(path);
            if (finfo.Exists)
            {
                if (finfo.LastWriteTime.Date < DateTime.Now.Date)
                {
                    string former_filename = path + "." + finfo.LastWriteTime.Date.ToString("%y-%M-%d");
                    FileInfo former_finfo = new FileInfo(former_filename);
                    if (former_finfo.Exists) File.Delete(former_filename);
                    File.Move(path, former_filename);
                    stream = finfo.Create();
                }
                else
                {
                    stream = finfo.Open(FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                }
            }
            else
            {
                stream = finfo.Create();
            }
            StreamWriter writer = new StreamWriter(stream);
            writer.WriteLine(logtext);
            writer.Close();
            stream.Close();
        }
    }
}
